<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
				</div>
				<div class="flex-end-center-wrap">
					<!--<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>-->
					<jk-search-input v-model="searchParams.machineCode" placeholder="设备" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-common-status v-if="item.keyName === 'auditState'" :status="scope.row.auditState"></jk-common-status>
						<span v-else-if="item.keyName === 'maxValue'">{{ `${scope.row.minValue}~${scope.row.maxValue}` }}</span>
						<div v-else-if="item.keyName === 'processList'">
							<div
								v-for="(processItem, processIndex) in scope.row.configItem || []"
								:key="processIndex"
							>{{ `${processIndex + 1}、${processItem.processName}: ${processItem.unitPrice}` }}</div>
						</div>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="250">
					<template slot-scope="scope">
						<div class="flex-center">
							<div class="item-gap"><el-button size="mini" icon="el-icon-delete" type="danger" @click="onDeleteBtn(scope.row)">删除</el-button></div>
							<div class="item-gap"><el-button size="mini" icon="el-icon-view" type="info" @click="onDetailBtn(scope.row)">详情</el-button></div>
							<el-button size="mini" type="primary" icon="el-icon-edit" @click="onEditBtn(scope.row)">编辑</el-button>
						</div>
						<!--<jk-operation-bar
							:scope="scope"
							api-prefix="machinemanager/lt-data-params"
							:api-item="apiItem"
							:permission-prefix="prefix"
							@on-detail="onDetailBtn"
							@on-edit="onEditBtn"
							@on-refresh="getListRequest"
						></jk-operation-bar>-->
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<save-dialog
			:page-type="pageType"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
	</div>
</template>

<script>
    import saveDialog from './save-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, tipsBar } from '/src/utils/jk/jk-tool';
    import {
        machineManagerLtDataParamsDelete,
        machineManagerLtDataParamsPage
    } from '/src/api/machine/ltGatherParamConfig';
    import { auditStateList } from '../../../utils/jk/status-option-list';
    import getMenuColumn from '../../../mixins/getMenuColumn';

    export default {
        name: 'LtGatherParamConfig',
        components: {
            saveDialog
        },
        mixins: [calHeight, getMenuColumn],
        data() {
            return {
                apiItem: {
                    'page': machineManagerLtDataParamsPage,
                    'delete': machineManagerLtDataParamsDelete
                },
                pageType: '',
                prefix: 'spin:yarnCountPriceConfig',
                auditStateList: auditStateList,
                dataId: null,
                workshopList: [],
                saveDialogState: false,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                totalCount: 0,
                processList: [],
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    machineCode: '',
                    auditState: null
                }
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onDeleteBtn(row) {
                this.$msgbox({
                    title: '消息',
                    message: `确认 "删除" ？`,
                    iconClass: 'el-icon-question',
                    showCancelButton: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    beforeClose: (action, instance, done) => {
                        if (action === 'confirm') {
                            instance.confirmButtonLoading = true;
                            instance.confirmButtonText = '执行中...';
                            this.apiItem.delete({ id: row.id }).then(res => {
                                instance.confirmButtonLoading = false;
                                instance.confirmButtonText = '确定';
                                if (res.data.status === 200) {
                                    this.getListRequest();
                                    tipsBar(this, 'custom', 'success', `删除成功!`);
                                    done();
                                }
                            });
                        } else {
                            done();
                        }
                    }
                });
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            async getDependentData() {
                this.tableLoading = true;
                // this.$set(this.searchParams, 'processCode', this.$store.state.user.userProcess.defaultProcessCode);
                this.processList = this.$store.state.user.userProcess.processList || [];
                await this.getMenuColumnListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            onDetailBtn(e) {
                this.pageType = 'detail';
                this.dataId = e.id;
                this.saveDialogState = true;
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.pageType = 'add';
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.pageType = 'edit';
                this.dataId = row.id;
                this.saveDialogState = true;
            }
        }
    };
</script>
